#bilibili用户系统


from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import tkinter as tk
from PIL import Image, ImageTk
import time
import threading
import project1
import encol
import reset
import contract_ui
import privacy_ui
from index import *
import hashlib
import tu_rc
import sys

# UI--Logic分离
enter, _ = loadUiType('enter.ui')




# 欢迎界面
def showWelcome():
    # 获取屏幕尺寸，使窗口位于屏幕中央
    width = 620
    heigh = 380
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    root.title('欢迎使用哔哩哔哩')
    #root.overrideredirect(True)  # 去除窗口边框
    root.attributes("-alpha", 1)  # 窗口透明度（1为不透明，0为全透明）
    root.geometry('%dx%d+%d+%d' % (width, heigh, (screenwidth - width) / 2, (screenheight - heigh) / 2))

    global photo
    # 设置条形框,插入图片
    image = Image.open(r'./g.jpg')
    photo = ImageTk.PhotoImage(image)

    Lab = tk.Label(root, text='哔哩哔哩( ゜ -゜)つロ 乾杯~', compound='center', font=('华文隶书', 28), image=photo)
    Lab.pack()  # 设置主界面


def closeWelcome():
    for i in range(3):
        time.sleep(1)
    root.destroy()


class EnterApp(QMainWindow,enter):


    #定义构造方法
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_buttons()
        self.show()


    def handle_buttons(self):
        self.enter_agree_Button.clicked.connect(self.handle_login)
        self.enter_enrol_Button.clicked.connect(self.encol)
        self.enter_change_password_Button.clicked.connect(self.reset)
        self.enter_xieyi_Button.clicked.connect(self.contract)
        self.enter_zhengce_Button.clicked.connect(self.privacy)


    def encol(self):
        self.encol_app = encol.EncolApp()
        self.close()
        self.encol_app.show()

    def reset(self):
        self.reset_app = reset.ResetApp()
        self.close()
        self.reset_app.show()

    def contract(self):
        self.contract_app = contract_ui.ContractApp()
        self.close()
        self.contract_app.show()

    def privacy(self):
        self.privacy_app = privacy_ui.PrivacyApp()
        self.close()
        self.privacy_app.show()


    # MD5加密处理
    def md5(self, arg):
        hash = hashlib.md5(bytes("禾木AI", encoding="utf-8"))
        hash.update(bytes(arg, encoding='utf-8'))
        return hash.hexdigest()

    def handle_login(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from users where user_num=%s and user_pwd=%s"
        user_num = self.enter_accounts_Edit.text()
        user_pwd = self.enter_password_Edit.text()
        pwd = self.md5(user_pwd)
        cur.execute(sql, (user_num, pwd))
        data = cur.fetchone()

        if data:
            self.main_app = project1.MainApp()
            self.close()
            self.main_app.show()
        else:
            self.statusBar().showMessage("账号或密码错误，登陆失败！")
            self.enter_accounts_Edit.setText('')
            self.enter_password_Edit.setText('')



def main():

    app = QApplication([])
    window = EnterApp()
    window.show()
    app.exec_()


if __name__ == '__main__':
    root = tk.Tk()
    tMain = threading.Thread(target=showWelcome)  # 开始展示
    tMain.start()
    t1 = threading.Thread(target=closeWelcome)  # 结束展示
    t1.start()
    root.mainloop()

    main()


